package com.huidu.bitrade.job;

import com.huidu.bitrade.constant.AdvertiseType;
import com.huidu.bitrade.constant.BooleanEnum;
import com.huidu.bitrade.entity.Advertise;
import com.huidu.bitrade.entity.Member;
import com.huidu.bitrade.entity.MemberWallet;
import com.huidu.bitrade.entity.Order;
import com.huidu.bitrade.exception.InformationExpiredException;
import com.huidu.bitrade.exception.ServiceException;
import com.huidu.bitrade.service.AdvertiseService;
import com.huidu.bitrade.service.MemberService;
import com.huidu.bitrade.service.MemberWalletService;
import com.huidu.bitrade.service.OrderService;
import com.huidu.bitrade.util.MessageResult;
import com.huidu.bitrade.vo.BusinessMoneyVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@Slf4j
@RequiredArgsConstructor
public class CheckSoldTask {

    private final OrderService orderService;
    private final AdvertiseService advertiseService;
    private final MemberService memberService;
    /**
     * 检测下架广告：前一天交易卖出大于买入的，自动下架 凌晨四点
     */
//    @Scheduled(cron = "0 0 4 * * *")
//    @Transactional(rollbackFor = Exception.class)
//    public void check() throws ParseException {
//        log.info("=========开始查询订单===========");
//        //查询所有承兑商每日出金，入金
//        List<BusinessMoneyVo> outList = orderService.getBusinessPrice(AdvertiseType.SELL);
//        List<BusinessMoneyVo> InList = orderService.getBusinessPrice(AdvertiseType.BUY);
//        Map<Long, BigDecimal> inMap = InList.stream().collect(Collectors.toMap(BusinessMoneyVo::getMemberId,BusinessMoneyVo::getMoney));
//        for (BusinessMoneyVo businessMoneyVo:outList){
//            BigDecimal money = inMap.get(businessMoneyVo.getMemberId());
//            if(Objects.nonNull(money) &&money.compareTo(businessMoneyVo.getMoney())>=0){
//                continue;
//            }
//            //卖出的比买入的多，修改商家发布广告状态
//            Member member = memberService.findOne(businessMoneyVo.getMemberId());
//            member.setPublishAdvertise(BooleanEnum.IS_FALSE);//不允许发布广告
//            memberService.save(member);
//        }
//    }

//    public void returnBalance(Advertise advertise) throws ServiceException {
//        MemberWallet memberWallet = memberWalletService.findByOtcCoinAndMemberId(advertise.getCoin(), advertise.getMember().getId());
//        MessageResult result = memberWalletService.thawBalance(memberWallet, advertise.getRemainAmount());
//        if (result.getCode() != 0) {
//            log.error("=========otc检测广告下架，解冻钱包失败========memberId{}",advertise.getMember().getId());
//            throw new ServiceException("钱包解冻失败");
//        }
//        //修改广告状态
//        int ret = advertiseService.putSystemOffShelves(advertise.getId(), advertise.getRemainAmount());
//        if (!(ret > 0)) {
//            throw new ServiceException("修改广告状态失败");
//        }
//    }
}
